<?php

if( !defined('BASE_PATH') )
{
	define('BASE_PATH_TMP', dirname(dirname(__FILE__) ) ); 
	require_once BASE_PATH_TMP.'/include.php';
	require_once BASE_PATH_TMP.'/Library/TextDealer.php';
	require_once BASE_PATH_TMP.'/Library/phpQuery.php';
}

class CrawlTool
{
	function __construct()
	{
		$this->_resetResult();
	}

	static $_instance = null;
	static function getInstance()
	{
		if( self::$_instance === null )
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	private function _resetResult()
	{ 
		global $result;
		$result = array(
			'title'=>'',
			'content'=>'',
			'img'=>array(),
			'img_desc'=>'',
		); 
	}

	function getModel( $model )
	{
		global $result, $query;
		$file = BASE_PATH.'/model/'.$model.'.php';
		if( is_file( $file ) )
		{
			$this->_resetResult();
			require_once $file;
			return true;
		}
		else
		{
			echo "need website parameter.\n";
			return false;
		} 
	}

	function deal()
	{ 
		global $result, $query;
		$query['history'] = array();
		if( !isset($query['url']) || !trim($query['url']) )
		{
			echo "url is null.";
			return false;
		}
		$query['next'] = $query['url'];
		$maxPageNum = 0;

		do
		{ 
			$query['url'] = $query['next'];
			$query['next'] = '';
			if( trim($query['url']) == '')
			{
				break;
			}
			$file = TextDealer::getInstance()->downloadUrl( $query['url'] );
			if( $file === false )
			{
				echo "download failed.". $query['url'];
				return false;
			}
			
			$doc = phpQuery::newDocumentFile( $file ); 
			if( !isset( $result['title'] )
				|| trim( $result['title'] ) == '')
			{
				phpQuery::each($doc[$query['title']], 'getTitle', new CallbackParam, new CallbackParam);
			}
			//phpQuery::each($doc[$query['content']], 'getImgDesc', new CallbackParam, new CallbackParam);
			phpQuery::each($doc[$query['content']], 'getContent', new CallbackParam, new CallbackParam);
			//phpQuery::each($doc[$query['video']], 'getVideo', new CallbackParam, new CallbackParam);
			phpQuery::each($doc[$query['multi']], 'getNextUrl', new CallbackParam, new CallbackParam); 

			if( $query['next'] 
				&&  !in_array( $query['next'], $query['history'] ) )
			{
				$query['history'][] = $query['url'];
				continue;
			}

			break;
		}
		while( $maxPageNum ++ < 20 ); 

		//var_dump($result);exit;
		return $result;
	}
}



function filte($key, $value, $filters)
{
    global $result, $query; 
	if( is_array( $filters ) )
	{ 
		foreach( $filters as $filter ) 
		{
			pq($filter)->remove();
		}
	}
}

function getTitle($key, $value)
{
    global $result, $query;
	filte($key, $value, $query['titlefilter']);
    $str = pq($value)->text();
	TextDealer::getInstance()->deal($str);
    $result['title'] = $str;
	/*
	$str = '三星Galaxy NoteII登基 神笔开启Note新时代';
	echo '-->'.strlen($str);
	echo mb_detect_encoding($str);
	echo $str;
	echo iconv('UTF-8', 'gb2312', $str);
	echo mb_convert_encoding($str, 'gb2312', 'UTF-8');
	//*/
}


function getContent($key, $value)
{
    global $result, $query;
	pq($value)->find('img')->before('[picture]');
	pq($value)->find('img')->each('getImgSrc');
	filte($key, $value, $query['contentfilter']);
    $str = pq($value)->text();
	TextDealer::getInstance()->deal($str);

	if ( isset( $result['img'] )
		&& $result['img'] )
	{
		$texts = explode('[picture]', $str);
		$imgs = array_slice($result['img'], -count($texts)+1);
		$img_txt = array();
		$img_txt[] = array_shift( $texts ); 
		foreach( $texts as $text )
		{
			$img_txt[] = array_shift( $imgs ); 
			$img_txt[] = $text; 
		}
		$img_txt = array_diff( $img_txt, array( '',' ' ) ); 

		if( !is_array($result['img_desc']) ) $result['img_desc'] = array();
		$result['img_desc'] = array_merge( $result['img_desc'], $img_txt ); 
	}

	$result['content'] .= str_replace('[picture]', '', $str);
}

function getImgSrc( $node )
{
    global $result, $query; 
	$img = pq($node)->attr('src');
	if( $img )
	{
		if( !preg_match( '/^http/', $img, $arr ) )
		{
			$index = strpos( $query['url'], '/', 10);
			$pre = substr( $query['url'], 0, $index);
			$img = $pre.$img;
		}
		$result['img'][] = $img;
	}	
}

function getImgDesc( $key, $value )
{
    global $result, $query; 
	pq($value)->find('img')->each('getImgSrc');
}

function getVideo( $key, $value )
{
    global $result, $query; 
	filte( $key, $value, $query['videofilter'] );
	$str = pq($value)->find('embed')->attr('flashvars');
	if( $str )
	{
		if( preg_match( '/vid=[a-zA-Z0-9]*/', $str, $arr ) && $arr[1] )
		{
			$result['video']['vid'] = $arr[1];
		}

		if( preg_match( '/pic=.*\.jpg/', $str, $arr ) && $arr[1] )
		{
			$result['video']['pic'] = $arr[1];
		} 
	} 
}

function getNextUrl( $key, $value )
{
    global $result, $query; 
	$url = pq($value)->attr('href');

	if( $url )
	{
		if( !preg_match( '/^http/', $url ) )
		{
			$arr = explode(substr($url, 0, 5), $query['url']);
			$url = $arr[0].$url;
		}
		$query['next'] = $url; 
	} 
}

if( isset($argv) && isset($argv[0]) && $argv[0] == basename( __FILE__ ) )
{
	global $query,$result;
	$obj = CrawlTool::getInstance();
	$obj->getModel( 'QQ' );

	if( isset($argv) && isset($argv[2]) && $argv[2] )
	{
		$query['url'] = $argv[2];
	}

	$result = $obj->deal();
	print_r($result); 
}
